
import { _decorator, Component, Node, director, find, EventTouch, Sprite, Label, RichText, SpriteFrame, SystemEvent, game } from 'cc';
import { CommandType, Message, MessageType } from './framework/Message';
import { MessageBase } from './framework/MessageBase';
import { MessageCenter } from './framework/MessageCenter';
import { GameData } from './GameData';
import { Utils } from './utils/Utils';
import { BattleManager } from './BattleManager';
import { Hero } from './player/Hero';
import { Pet } from './player/Pet';
import { Move } from './player/Move';
import { Item } from './player/Item';
import { Map } from './map/Map';
import { MiniMap } from './map/MiniMap';
import { EventManager } from './EventManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = UIManager
 * DateTime = Sat Feb 05 2022 15:05:09 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = UIManager.ts
 * FileBasenameNoExtension = UIManager
 * URL = db://assets/script/UIManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('UIManager')
export class UIManager extends MessageBase {
    static Instance: UIManager;

    // 选项光标所在的节点
    _t: Node = null;
    // 四种选项所在的节点
    _list: Node = null;

    // 当前光标对应索引位置 0 1 2 3
    index: number;
    // 可选的4种选项
    type: number[];
    faction: number[];
    move: number[];
    // 职业/势力/技能 在对应总表中的索引
    tid: number[];
    // 已选择
    hadChoice: number[];

    // 对应的4张图片帧
    frames: SpriteFrame[] = [];

    onLoad() {
        super.onLoad();
        UIManager.Instance = this;

        this._t = this.node.getChildByPath("Select");
        this._list = this.node.getChildByName("List");

        for (let i = 0; i < 4; i++) {
            let _c = this._list.children[i];
            _c.on(SystemEvent.EventType.TOUCH_END, this.handleClick, this);
        }
        // console.log("UI 组件的节点没有激活时，组件也不会激活");

    }

    init() {
        // 默认设置成 UI 管理，如果想设置成其他类型，直接重写此方法即可(不调用父类的默认设置为UI了)
        this.messageType = MessageType.TYPE_UI;
    }

    initData() {
        this.index = 0;
        this.type = [];
        this.faction = [];
        this.move = []
        this.tid = []
        this.hadChoice = [];
    }

    ReveiveMessage(message: Message) {
        super.ReveiveMessage(message);
        if (message.Command == CommandType.COM_LOADING_COMPLETED) {
            this.initData()
            this.startSelect();
        }
    }

    startSelect() {
        // 激活当前节点
        this.node.active = true;
        console.log(this.node);

        // 初始化选项
        this.choiceOther();
        // 蓝色选择框默认处于 0 下标位置
    }

    choiceOther() {
        // 依次选择 职业 势力 初始技能
        let _d = GameData.Instance;
        let _s: string | any[], _f: any[], _i: any[];
        switch (this.hadChoice.length) {
            case 0:
                _i = this.type;
                _s = _d.heroTips;
                _f = _d.heroFrames;
                break;
            case 1:
                _i = this.faction;
                _s = _d.factionTips;
                _f = _d.factionFrames;
                break;
            case 2:
                _i = this.move;
                _s = _d.moveTips;
                _f = _d.moveFrames;
                break;
            default:
                console.log("choice error");
                break;
        }
        // 抽取4个选项
        this.tid = <number[]>Utils.getRandomRange(0, _s.length - 1, 4);
        // 取出4个选项对应的描述信息数组和图片帧
        for (let i = 0; i < 4; i++) {
            _i[i] = _s[this.tid[i]];
            this.frames[i] = _f[this.tid[i]];
            // 替换图片
            (<Sprite>this._list.children[i].getComponent("cc.Sprite")).spriteFrame = this.frames[i];

            // 角色和势力的描述信息以数组形式存储的
            (<Label>this._list.children[i].children[0].getComponent("cc.Label")).string = _i[i][0];

            (<Label>this._list.children[i].children[1].getComponent("cc.Label")).string = _i[i][1];
        }

        // 光标移回初始位置
        this._t.setPosition(-340, -200, 0);
        // 当前默写选择0
        this.index = 0;
        // 激活详情节点中的名称和描述节点
        let _c = this._list.children[this.index];
        _c.children[0].active = true;
        _c.children[1].active = true;

        // 激活选项节点，开始选择职业 势力 技能
        this._t.active = true;
        // 激活详情节点
        this._list.active = true;
    }
    handleClick(e: EventTouch) {
        // 恰好是当前点击的选项的下标
        let _i = Number((<Node>e.target).name);

        if (_i == this.index) {
            // 当前选择的节点
            let _c = this._list.children[_i];
            switch (this.hadChoice.length) {
                case 0:
                    MessageCenter.SendCustomMessage(MessageType.TYPE_HERO, CommandType.COM_UI_CHOICE_HERO, this.tid[_i])
                    this.hadChoice[0] = this.tid[_i];
                    break;
                case 1:
                    MessageCenter.SendCustomMessage(MessageType.TYPE_HERO, CommandType.COM_UI_CHOICE_FACTION, this.tid[_i])
                    this.hadChoice[1] = this.tid[_i];
                    break;
                case 2:
                    MessageCenter.SendCustomMessage(MessageType.TYPE_MOVE, CommandType.COM_UI_CHOICE_MOVE, this.tid[_i])
                    // 关闭选项节点，开始选择职业 势力 技能
                    this._t.active = false;
                    // 关闭详情节点
                    this._list.active = false;
                    // 关闭当前UI节点
                    this.node.active = false;

                    // 激活英雄、技能、地图节点
                    MessageCenter.SendCustomMessage(MessageType.TYPE_HERO, CommandType.COM_HERO_INIT)
                    MessageCenter.SendCustomMessage(MessageType.TYPE_MOVE, CommandType.COM_MOVE_INIT)
                    MessageCenter.SendCustomMessage(MessageType.TYPE_MAP, CommandType.COM_MAP_INIT);

                    // 同样的激活物品、宠物面板
                    MessageCenter.SendCustomMessage(MessageType.TYPE_ITEM, CommandType.COM_ITEM_INIT)
                    MessageCenter.SendCustomMessage(MessageType.TYPE_PET, CommandType.COM_PET_INIT)
                    return;
                default:
                    console.log("选择错误");
                    break;
            }
            // 隐藏当前选项的两个描述文本节点
            _c.children[0].active = false;
            _c.children[1].active = false;
            // 如果没进行3个步骤的选择，则开始选下一项
            if (this.hadChoice.length < 2) {
                this.choiceOther()
            }
        } else {
            // 移动蓝色选框：获取当前选框的位置
            let pos = this._t.getPosition();
            // 当前点击的节点和上一次选择的节点的位置差
            pos.x += (_i - this.index) * 200;
            // 更新蓝色选框位置
            this._t.setPosition(pos);
            this.changeDesc(_i)
        }
    }
    changeDesc(cur) {
        // 需要先隐藏选择过的选项的 type desc
        let _c = this._list.children[this.index];
        _c.children[0].active = false;
        _c.children[1].active = false;
        // 再点亮当前选择的节点的 type desc
        _c = this._list.children[cur];
        _c.children[0].active = true;
        _c.children[1].active = true;
        // 保存当前选项
        this.index = cur;
    }
    gameOver(ifWin) {
        if (ifWin) {
            console.log("赢了");
        } else {
            console.log("输了");
        }
        console.log("再来一局");

        Hero.Instance.node.active = false;
        Pet.Instance.node.active = false;
        Move.Instance.node.active = false;
        Item.Instance.node.active = false;
        Map.Instance.node.active = false;
        MiniMap.Instance.node.active = false;
        BattleManager.Instance.node.active = false;
        EventManager.Instance.node.active = false;

        MessageCenter.SendCustomMessage(MessageType.TYPE_UI, CommandType.COM_LOADING_COMPLETED);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
